#pragma once
#include "logger.hpp"
#include "inet_addr.hpp"
#include <vector>
#include <iostream>

using namespace logger_module;

class route
{
private:
    bool ifexist(InetAddr& man)
    {
        for(auto&user:_online_user)
        {
            if(man==user)  return true;
        }
        return false;
    }
    void enroll(InetAddr& user)
    {
        LOG(log_level::INFO)<<"用户"<<user.addr_name()<<" 注册";
        _online_user.push_back(user);
    }
public:
    route(){}
    ~route(){}
    void message_send(int sockfd,const std::string& message,InetAddr& peer)
    {
        if(!ifexist(peer))
        {
            enroll(peer);
        }
        std::string send_message=peer.addr_name();
        send_message+="# "+message;
        //send message to everyone
        LOG(log_level::DEBUG)<<"收到了"<<send_message;
        for(auto&user:_online_user)
        {
        sendto(sockfd,send_message.c_str(),send_message.size(),0,(struct sockaddr*)&user.Addr(),sizeof(user.addr_name()));
        }
    }
private:
    std::vector<InetAddr> _online_user;
};